package com.zzzyh.springproject.controller;


import com.zzzyh.springproject.pojo.APIResponse;
import com.zzzyh.springproject.pojo.Admin;
import com.zzzyh.springproject.service.AdminService;
import com.zzzyh.springproject.utils.JwtUtil;
import com.zzzyh.springproject.utils.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author：郑雨杭
 * @date：2025-01-02 14:57:04
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

//    功能一：完成登录功能接口
    @RequestMapping("/login")
    @ResponseBody
    public APIResponse<Token> login(@RequestBody Admin admin){
        Admin inputAdmin = adminService.getAdminByUserName(admin.getUsername());
        if(inputAdmin == null)
            return APIResponse.error(2,"用户名或密码错误");
        admin.setSalt(inputAdmin.getSalt());
        if(adminService.login(admin)){
            return APIResponse.success(new Token(JwtUtil.generateToken(admin.getUsername(),1000*60*60*1)));
        }else{
            return APIResponse.error(2,"用户名或密码错误");
        }
    }

    @RequestMapping("/getAdminByUserName")
    @ResponseBody
    public APIResponse<Admin> getAdminByUserName(String userName){
        return APIResponse.success(adminService.getAdminByUserName(userName));
    }

}
